<?php

namespace App\Http\Controllers\Api;

use App\Consts\GlobalUserRecommendConst;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class QuickController extends BaseController
{
    public $validate = [
        "preview" => ["rules" => [
            "url" => "required|pattern_domain|max:1000",
        ]],
        "collect" => ["rules" => [
            "url" => "required|pattern_domain|max:1000",
            "package_id" => "required|string|max:24",
            'is_recommend' => 'in:0,1'
        ]],
    ];

    /**
     * @path /quick/preview
     * @category 桌面
     * @title 【有备注】一键收藏【未加入收藏夹】
     * @method POST
     * @params [{"name":"url","type":"text","required":1,"example":"","desc":"一键收藏url"}]
     * @resBody {"success":true,"statusCode":200,"result":{"title":"文章标题","description":"文章摘要，可能位空值","cover_url":"封面图，可能位空值"},"message":"OK"}
     * @description 1.【前端】只需要校验是否符合http开头即可
     */
    public function preview(Request $request)
    {
        $params = $request->all();

        $data = service()->QuickService->onPreview($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /quick/collect
     * @category 桌面
     * @title 收藏文章【发生于一键收收藏后】
     * @method POST
     * @params [{"name":"url","type":"text","required":1,"example":"","desc":"一键收藏url"},{"name":"package_id","type":"text","required":1,"example":"","desc":"收藏夹ID"},{"name":"is_recommend","type":"text","required":0,"example":"","desc":"是否同时推荐，0否，1是（默认）"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"文章ID","title":"文章标题","cover_url":"文章封面图，如1.jpg","description":"文章描述"},"message":"OK"}
     */
    public function collect(Request $request)
    {
        $params = $request->all();
        if (!array_key_exists('is_recommend', $params)) {
            $params['is_recommend'] = GlobalUserRecommendConst::USER_IS_RECOMMEND;
        }

        $data = service()->QuickService->onCollect($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /quick/catch
     * @category 桌面
     * @title 【后台使用】抓取文章
     * @method POST
     * @params [{"name":"url","type":"text","required":1,"example":"","desc":"一键收藏url"},{"name":"phone","type":"text","required":1,"example":"","desc":"手机号"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"id":"文章ID","author_id":"用户ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章摘要","type":"类型，1推荐，2原创","is_deleted":0,"is_shield":0,"created_at":"创建时间戳","author":{"id":"用户ID","avatar":"用户头像，如1.jpg","nickname":"用户昵称"}}}
     */
    public function catch(Request $request)
    {
        $params = $request->all();

        $data = service()->QuickService->onCatch($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }
}
